package com.example.qixin0314.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qixin0314.Nat.BaseApplication;
import com.example.qixin0314.Nat.selectCompany.Values4;
import com.example.qixin0314.Nat.selectCompany.selectCompany;
import com.example.qixin0314.R;
import com.example.qixin0314.bean.listAdapter;
import com.example.qixin0314.bean.listModel;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import cn.leancloud.LCObject;
import cn.leancloud.LCQuery;
import cn.leancloud.LCUser;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SelectActivity extends AppCompatActivity {
    private EditText edit_select;

    private TextView textFanHui;

    RecyclerView listview;
    LCUser currentUser=LCUser.getCurrentUser();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select);
        initView();
        Aedit_select();
        initData();
    }

    private void initData() {
        textFanHui=findViewById(R.id.text_fanHui);
        textFanHui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectActivity.this.finish();
            }
        });
    }

    private void initView() {
        edit_select=findViewById(R.id.edit_select);
        listview=findViewById(R.id.listview);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        listview.setLayoutManager(llm);
        List<listModel>data=new ArrayList<>();
        listAdapter adapter=new listAdapter(SelectActivity.this);
        adapter.setData(data);
        listview.setAdapter(adapter);
        if (currentUser!=null){
            LCQuery<LCObject> query = new LCQuery<>("lll");
            query.whereEqualTo("id", currentUser.getObjectId());
            query.findInBackground()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<List<LCObject>>() {
                        @Override
                        public void onSubscribe(@NotNull Disposable d) {

                        }

                        @Override
                        public void onNext(@NotNull List<LCObject> lcObjects) {
                            Toast.makeText(SelectActivity.this,"1",Toast.LENGTH_SHORT).show();
                            for (int i = 0; i < lcObjects.size(); i++) {
                                listModel model=new listModel();
                                model.setName(lcObjects.get(i).get("name").toString());
                                data.add(model);
                            }
                            adapter.notifyDataSetChanged();
                        }

                        @Override
                        public void onError(@NotNull Throwable e) {
                            Toast.makeText(SelectActivity.this,"2",Toast.LENGTH_SHORT).show();

                        }

                        @Override
                        public void onComplete() {

                        }
                    });

        }
    }
    private void Aedit_select(){//键盘确定监听
        edit_select.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                String key=edit_select.getText().toString();
                if (!key.isEmpty()){
                    Select(key);
                    if (currentUser!=null){
                        Acreate(key);
                    }
                }else {
                    Toast.makeText(SelectActivity.this,"请输入要查询的数据",Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });
    }
    private void Select(String key){
        Call<selectCompany<Values4>> call = BaseApplication.httpbinService.SelectCompany(key);
        call.enqueue(new Callback<selectCompany<Values4>>() {
            @Override
            public void onResponse(Call<selectCompany<Values4>> call, Response<selectCompany<Values4>> response) {
                String s = response.body().getValues().getRedirect_shareurl();
                if (!s.isEmpty()){
                    Intent intent=new Intent(SelectActivity.this,CompanyActivity.class);
                    intent.putExtra("uri",s);
                    intent.putExtra("name",response.body().getValues().getRedirect_title());
                    startActivity(intent);
                }else {
                    Toast.makeText(SelectActivity.this,"未查询到相关数据",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<selectCompany<Values4>> call, Throwable t) {

            }
        });
    }
    private void Acreate(String key){
        LCObject todo=new LCObject("lll");
        todo.put("name",key);
        todo.put("id",currentUser.getObjectId());
        todo.saveInBackground()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LCObject>() {
                    @Override
                    public void onSubscribe(@NotNull Disposable d) {
                    }
                    @Override
                    public void onNext(@NotNull LCObject lcObject) {
                        Toast.makeText(SelectActivity.this,"已添加到历史搜索",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onError(@NotNull Throwable e) {
                        Toast.makeText(SelectActivity.this,"添加失败",Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onComplete() {

                    }
                });



    }
}